//33.012991, -117.142756

var gs, map, lockOnPlane, planeLoc, planeIcon;
var planeMarker, headingBug, gsMarker; //markers
var gs_lat,gs_lon;
var hdgbug_lat,hdgbug_lon;

function initialize() {
	try {
		gs_lat = 33.012991;
		gs_lon = -117.142756;
		hdgbug_lat = gs_lat;
		hdgbug_lon = gs_lon;
		gs = new google.maps.LatLng(gs_lat, gs_lon);
		var myOptions = {
			zoom : 17,
			minZoom: 14,
			maxZoom: 19,
			center : gs,
			streetViewControl: false,
			mapTypeId : 'terrain'//google.maps.MapTypeId.SATELLITE
		};
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		planeLoc = new google.maps.LatLng(plane_lat,plane_lon);
		
		gsMarker = new google.maps.Marker({
			map: map,
			draggable: false,
			clickable: false,
			position: gs,
			icon: "img/tower-small.gif"
		});
		headingBug = new google.maps.Marker({
			map: null,
			draggable: true,
			clickable: true,
			position: new google.maps.LatLng(hdgbug_lat,hdgbug_lon)
		});
				
		_makePlaneIcons();
		
		planeMarker = new google.maps.Marker({
		    map:map,
		    draggable: false,
		    clickable: false,
		    position: planeLoc,
		    icon: planeIcon[7]
		  });		
		setInterval("_gmLooping()",100);
	} catch (e) {
		alert("In initialize() -> "+e);
	}
}

function _makePlaneIcons()
{
	try
	{
		planeIcon = new Array();
		for (var i=0;i<72;i++)
		{
			planeIcon[i] = {
				url: 'img/plane/planeH'+(i*5)+'.png',
				anchor: new google.maps.Point(150,150),
				scaledSize: new google.maps.Size(75, 75),
				origin: new google.maps.Point(-112,-112)
			};
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function _gmUpdatePlane()
{
	if (plane_lat==0 && plane_lon==0){return;}
	planeLoc = new google.maps.LatLng(plane_lat,plane_lon);
	if (planeMarker!=null)
	{
		var dir,nn;
		planeMarker.setPosition(planeLoc);
		if (plane_heading!=null){dir = plane_heading;}
		else if (plane_course!=null){dir = plane_course;}
		else{dir = 0;}
		if (map.getHeading()==null)
		{nn = Math.round(plane_heading/5);}
		else
		{nn = Math.round((plane_heading-map.getHeading())/5);}
		nn %= 72;
		if (nn<0){nn=72-nn;}
		planeMarker.setIcon(planeIcon[nn]);
	}
}

function gmGoHome()
{
	_gmUnlock();
	map.setCenter(gs);
}
function gmGotoPlane()
{
	_gmUnlock();
	map.setCenter(planeLoc);
}
function gmLockOnPlane()
{
	lockOnPlane = true;
	claimLock();
}
function _gmLooping()
{
	try
	{
		_gmUpdatePlane();
		if (lockOnPlane)
		{
			map.setCenter(new google.maps.LatLng(plane_lat,plane_lon));
			if (plane_heading!=null)
			{
				map.setHeading(Math.floor(plane_heading));
			}
			else if (plane_course!=null)
			{
				map.setHeading(Math.floor(plane_course));
			}
			map.setOptions({
				draggable: false,
				panControl: false,
				scaleControl: false
			});
		}
		
		if (headingBug.getMap()!=null)
		{
			hdgbug_lat = headingBug.getPosition().lat();
			hdgbug_lon = headingBug.getPosition().lng();
		}
	}
	catch (e)
	{
		alert("_gmLooping() -> "+e);
	}
}
function _gmUnlock()
{
	lockOnPlane = false;
	map.setOptions({
		draggable: true,
		panControl: true,
		scaleControl: true
	});
	try{lockReleased();}
	catch (e){
		alert(e);
	}
}
function gmToggleHeadingBug()
{
	if (headingBug.getMap() == null)
	{
		headingBug.setMap(map);
		if (!lockOnPlane){gmGotoBug();}
		bugVisible();
	}
	else
	{
		headingBug.setMap(null);
		bugHidden();
	}
}
function gmGotoBug()
{
	_gmUnlock();
	map.setCenter(headingBug.getPosition());
}

function gmChangeGSpos(lat, lon)
{
	gs_lat = lat;
	gs_lon = lon;
	gs = new google.maps.LatLng(gs_lat, gs_lon);
	gsMarker.setPosition(gs);
	gmGoHome();
}
function gmMoveBugToPlane()
{
	if (plane_lat==0 && plane_lon==0){return;}
	headingBug.setPosition(new google.maps.LatLng(plane_lat,plane_lon));
	gmGotoBug();
}
function gmMoveBugToGS()
{
	headingBug.setPosition(new google.maps.LatLng(gs_lat,gs_lon));
	gmGotoBug();
}
